<?php

$oid = "";
$channel_id;

if(isset($_COOKIE["ombid"]))
{
	$id =  $_COOKIE["ombid"];
	$person = new Person($id);
	if(strlen($person->screen_name) == 0 && !isset($_GET["stop"]))
	{
		header('Location: '.HOME.'user.php?id='.$id.'&stop=true');
	}
}
//Begin Openid
if ($_POST['openid_action'] == "login"){ // Get identity from user and redirect browser to OpenID Server
	$openid = new OpenIDService();
	$openid->SetIdentity($_POST['openid_url']);
	$openid->SetTrustRoot('http://' . $_SERVER["HTTP_HOST"]);
	$openid->SetRequiredFields(array('email','fullname'));
	if ($openid->GetOpenIDServer()){
		$openid->SetApprovedURL(HOME.'index.php');  	// Send Response from OpenID server to this script
	
		//$openid->SetApprovedURL('http://localhost/openmb/');  	// Send Response from OpenID server to this script

		$openid->Redirect(); 	// This will redirect user to OpenID Server
	}else{
		$error = $openid->GetError();
		echo "ERROR CODE: " . $error['code'] . "<br>";
		echo "ERROR DESCRIPTION: " . $error['description'] . "<br>";
	}
	exit;
}
else if($_GET['openid_mode'] == 'id_res'){ 	// Perform HTTP Request to OpenID server to validate key
	$openid = new OpenIDService();
	$openid->SetIdentity($_GET['openid_identity']);
	$openid_validation_result = $openid->ValidateWithServer();
	if ($openid_validation_result == true){ 		// OK HERE KEY IS VALID
	    $oid = $_GET['openid_identity'];
  	}else if($openid->IsError() == true){			// ON THE WAY, WE GOT SOME ERROR
		$error = $openid->GetError();
		echo "ERROR CODE: " . $error['code'] . "<br>";
		echo "ERROR DESCRIPTION: " . $error['description'] . "<br>";
	}else{											// Signature Verification Failed
		echo "INVALID AUTHORIZATION";
	}
}else if ($_GET['openid_mode'] == 'cancel'){ // User Canceled your Request
	echo "USER CANCELED REQUEST";
}
//End Openid

function IDGet($oid)
{
	$id = "";
	if(isset($_COOKIE["ombid"]))
	{
		$id = $_COOKIE["ombid"];
	}
	else
	{
		$id = Person::UIDFromOID($oid);
	}
	if(strlen($oid) > 0 && strlen($id) == 0)
	{
		$person = new Person();
		$id = $person->Create($oid);
	}
	
    return $id; 	
}



?>
